//
//  WCG_O_DetailProviderViewController.m
//  Project_B
//
//  Created by 黑黑 on 15/11/26.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//

#import "WCG_O_DetailProviderViewController.h"
#import "GXProviderDetailTableViewCell.h"
#import "WCG_O_Detail_DetailProviderViewController.h"
@interface WCG_O_DetailProviderViewController () <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIImageView *image;
@property (nonatomic, strong) UILabel *titleLable;
@property (nonatomic, strong) UILabel *subtitleLable;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UITableView *myTableView;
@end

@implementation WCG_O_DetailProviderViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self getProviderDetailData];
    [self createTableView];
    [self createHeaderView];
    [self loadNightMBProgress];
    //显示底部按钮
    [self createButtomBackButtonIsHiddenShareBtn:YES];
    [self.backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.contentInset = UIEdgeInsetsMake(128, 0, 0, 0);
    [self.tableView registerClass:[GXProviderDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
}

- (void)createHeaderView
{
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -128, kScreenWidth, 128)];
    [self.tableView addSubview:self.headerView];
    [self.headerView setBackgroundColor:[UIColor colorWithRed:40/255.0 green:43/255.0 blue:51/255.0 alpha:1]];
    
    self.image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    self.image.layer.cornerRadius = 20;
    [self.image sd_setImageWithURL:self.providerModel.avatar placeholderImage:[UIImage sd_animatedGIFNamed:@"loading"]];
    self.image.contentMode = UIViewContentModeScaleAspectFill;
    self.image.layer.masksToBounds = YES;
    [self.headerView addSubview:self.image];
    [self.image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.headerView.mas_centerY);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(self.headerView.mas_left).with.offset(20);
    }];
    self.titleLable = [[UILabel alloc] initWithFrame:CGRectZero];
    self.titleLable.font = [UIFont systemFontOfSize:13];
    self.titleLable.textColor = [UIColor whiteColor];
    [self.titleLable setText:self.providerModel.screen_name];
    [self.headerView addSubview:self.titleLable];
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.image.mas_top).with.offset(0);
        make.left.mas_equalTo(self.image.mas_right).with.offset(20);
        make.right.mas_equalTo(self.headerView.mas_right).with.offset(-20);
        make.height.mas_equalTo(20);
    }];
    self.subtitleLable = [[UILabel alloc] initWithFrame:CGRectZero];
    self.subtitleLable.numberOfLines = 0;
    self.subtitleLable.textColor = [UIColor whiteColor];
    [self.subtitleLable setText:self.providerModel.provider_description];
    self.subtitleLable.font = [UIFont systemFontOfSize:11];
    [self.headerView addSubview:self.subtitleLable];
    [self.subtitleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLable.mas_bottom).with.offset(-5);
        make.left.mas_equalTo(self.image.mas_right).with.offset(20);
        make.bottom.mas_equalTo(self.headerView.mas_bottom).with.offset(-5);
        make.right.mas_equalTo(self.headerView.mas_right).with.offset(-20);
    }];
}

#pragma mark - 请求数据
- (void)getProviderDetailData {
    NSString *url = [NSString stringWithFormat:kProviderDetailWeb, self.providerModel.provider_id, 1L];
    [GXiaNetHandle getUrl:url cookie:kProviderDetailCookie success:^(id result) {
#if 0
        NSData *data = [NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingPrettyPrinted error:nil];
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
#endif
        self.dataArr = [GXProviderDetailModel transformWithArray:result[@"items"]];
        // 刷新tableView
        [self.tableView reloadData];
        [self.hud removeFromSuperview];
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - tableView协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GXProviderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.detailModel = self.dataArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WCG_O_Detail_DetailProviderViewController *itemVC = [[WCG_O_Detail_DetailProviderViewController alloc] init];
    itemVC.detailModel = self.dataArr[indexPath.row] ;
    itemVC.link = [self.dataArr[indexPath.row] link];
    itemVC.titleStr = [self.dataArr[indexPath.row] title];
    [self.navigationController pushViewController:itemVC animated:YES];
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    [self createHeaderView];
//    return self.headerView;
//}
//
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - backAction
- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
}

#pragma mark - scrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [UIView animateWithDuration:1 animations:^{
        self.backBtn.alpha = 0;
    }];
    NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y;//加上导航栏高度,第一次是-128
    NSLog(@"y2 === %f",y);
    if (y < -128) {
        CGRect frame = self.headerView.frame;
        frame.origin.y = y;//imageView的frame是不断往上偏移
        frame.size.height =  -y;//tablview向下偏移了多少,高度就增加多少
        self.headerView.frame = frame;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [UIView animateWithDuration:1 animations:^{
        self.backBtn.alpha = 0.8;
    }];
}

#pragma mark - 空白数据时的代理方法
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    return [UIImage imageNamed:@"icon-search-article"];
}

#pragma mark - 当数据为空时显示的字符
-(NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"数据走丢啦";
    NSDictionary *str = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f]};
    return [[NSAttributedString alloc] initWithString:text attributes:str];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
